package com.acme.bankapp.domain.bank;

/**
 * Constructor expects nonNegative balance
 * @author Java Student
 *
 */
public abstract class AbstractAccount implements Account, Comparable<Account>{

	/**
	 * Default serial
	 */
	private static final long serialVersionUID = 1L;
	protected double balance;
	private long id;
	private volatile static long counter = 0;
	
	public AbstractAccount() {
		AbstractAccount.counter++;
		this.id = AbstractAccount.counter;
	}

	public AbstractAccount(double balance) throws IllegalArgumentException {
		this();
		if (balance < 0)
			throw new IllegalArgumentException("Negative balance");
		this.balance = balance;
	}

	public double getBalance() {
		return this.balance;
	}

	public void deposite(double amount) {
		this.balance += amount;
	}
	
	/**
	 * @return round balance value
	 */
	public long desimalValue() {
		return Math.round(this.getBalance());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "AbstractAccount[balance=" + balance + "]";
	}
	
	@Override
	public int compareTo(Account account) {
		if (this.getBalance() > account.getBalance())
			return 1;
		else 
			return -1;
	}
}